import { getDateTime } from '../utils/tools.js'

/**
 * 店铺相关
 * TODO 店铺的激活需要联网
 */
const DP_KEY = 'bbs5-dp-key-241213'
export const saveDpInfo = (data) => {
  uni.setStorageSync(DP_KEY, data)
}
export const getDpInfo = () => {
  return uni.getStorageSync(DP_KEY) || { price: 30 }
}

/**
 * 会员卡相关
 */
const CARD_KEY = 'bbs5-card-key-241213'
export const saveCard = (data) => {
  const list = getCardList()
  // list.push(data)
  uni.setStorageSync(CARD_KEY, [...list, data])
}
export const getCardList = () => {
  const list = uni.getStorageSync(CARD_KEY) || []
  return list
}
export const deleteCard = (id) => {
  const list = getCardList()
  const res = list.filter(item => item.id != id)
  uni.setStorageSync(CARD_KEY, res)
  
  /* const index = list.findIndex(item => item.id == id)
  list.splice(index, 1)
  uni.setStorageSync(CARD_KEY, list) */
}
export const updateCard = (newItem) => {
  const list = getCardList()
  const index = list.findIndex(item => item.id == newItem.id)
  list.splice(index, 1, newItem)
  uni.setStorageSync(CARD_KEY, list)
}
/* 格式化会员卡列表给下拉菜单使用 */
export const getCardList4Select = () => {
  const list = getCardList()
  return list.map(item => ({ value: item.id, text: item.name }))
}
export const getCardById = (id) => {
  const list = getCardList()
  return list.find(item => item.id == id)
}

/**
 * 会员相关
 */
const MEMBER_KEY = 'bbs5-member-key-241213'
export const saveMember = (data) => {
  const list = getMemberList()
  uni.setStorageSync(MEMBER_KEY, [...list, data])
}
export const getMemberList = () => {
  const list = uni.getStorageSync(MEMBER_KEY) || []
  list.sort((item1, item2) => item2.id - item1.id)
  return list
}
export const getMemberById = (id) => {
  const list = getMemberList()
  return list.find(item => item.id == id)
}
export const updateMemberCount = (mid, count, type) => { // 0: 充值，1: 核销
  const member = getMemberById(mid)
  const list = getMemberList()
  const index = list.findIndex(item => item.id == mid)
  list.splice(index, 1, {...member, count: type ? member.count - count : member.count + count})
  uni.setStorageSync(MEMBER_KEY, list)
  // console.log(member, count, type, index)
}
export const getMemberBySearch = (value) => {
  const list = getMemberList()
  const res = list.filter(item => item.name.indexOf(value) > -1 || item.mobile.indexOf(value) > -1)
  return res
}
export const deleteMemberAndHistory = (mid) => {
  let cache = {
    mList: [],
    hList: []
  }
  const mList = getMemberList()
  const hList = getHistoryList()
  cache.mList = [...mList]
  cache.hList = [...hList]
  
  try {
    // 会员
    // const mList = getMemberList()
    const index = mList.findIndex(item => item.id == mid)
    mList.splice(index, 1)
    uni.setStorageSync(MEMBER_KEY, mList)
    // mList.name.name
    // 历史
    // const hList = getHistoryList()
    // const mhList = hList.filter(item => item.mid == mid)
    const otherHList = hList.filter(item => item.mid != mid)
    uni.setStorageSync(HISTORY_KEY, otherHList)
    // console.log(mid, mList, index, hList, otherHList)
  } catch(e) {
    uni.setStorageSync(MEMBER_KEY, cache.mList)
    uni.setStorageSync(HISTORY_KEY, cache.hList)
    uni.showModal({
      title: '系统提示',
      content: '删除失败，数据已恢复！',
      showCancel: false
    })
  }
}

/**
 * 充值，核销历史相关
 */
const HISTORY_KEY = 'bbs5-history-key-241213'
export const saveHistory = (data) => {
  const list = getHistoryList()
  uni.setStorageSync(HISTORY_KEY, [...list, data])
}
export const getHistoryList = (mid) => {
  if (mid) {
    const list = uni.getStorageSync(HISTORY_KEY) || []
    list.sort((item1, item2) => item2.id - item1.id)
    return list.filter(item => item.mid == mid)
  } else {
    return uni.getStorageSync(HISTORY_KEY) || []
  }
}
export const getLastCzDate = (mid) => {
  const list = getHistoryList(mid)
  const czList = list.filter(item => item.type == 0) // 0: 充值
  czList.sort((item1, item2) => item2.id - item1.id)
  return getDateTime(czList.shift().id)
}
export const getSummary = (mid) => {
  const list = getHistoryList(mid)
  const czList = list.filter(item => item.type == 0)
  const hxList = list.filter(item => item.type == 1)
  let price = 0, czCount = 0
  czList.forEach(item => {
    price += item.price
    czCount += item.count
  })
  /* hxList.forEach(item => {
    hxCount += item.count
  }) */
  const hxCount = hxList.reduce((total, item) => total + item.count, 0)
  return { price, czCount, hxCount }
}